package com.usian.controller;

import com.usian.pojo.User;
import com.usian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request, HttpSession session){
        User user1=userService.selectByName(user);
        if(user1!=null){
            if(user1.getRoleid()==1){
                String username = request.getParameter("username");
                session.setAttribute("username",username);
                return "redirect:/etc/findAll";
            }else {
                String username = request.getParameter("username");
                session.setAttribute("username",username);
                return "redirect:/etc/selectAll";
            }
        }else {
            return "login";
        }
    }
    @RequestMapping("toInsert")
    public String toInsert(){
        return "addUser";
    }
    @RequestMapping("insertUser")
    public String insertUser(User user){
        userService.add(user);
        return "login";
    }
}
